import { kdutils } from "kdweb-core/lib/utils";
import { ItemDefine, ItemID } from "../../../kds-base-define/src/ItemDefine";
import { ServerValues } from "../../../kds-base-define/src/ServerConfig";
import { DB } from "../db";
import { ItemHelper } from "../helper/ItemHelper";

import md5 = require("md5")
import { RpcUserInfo } from "./user";
import { Rpc } from "../rpc";

let db = DB.get(ServerValues.dbLobby)
let tableName = "t_user_items"
let redis = DB.redis

type dataType = {
	userID:number,
	items:ItemDefine.ItemPOData[],
}
function getItemKey(userID,itemID,count) {
	let str = "TIME:" + kdutils.getMillionSecond() + "USERID:" + userID + "ITEMID:" + itemID + "COUNT:" + count 
	return md5(str)
}
async function getData(h:string,userID:number) {
	let data:dataType = await db.getSingle(tableName,{userID:userID})
	return data 
}
async function getAllItems(h:string,userID:number) {
	let data = await getData(h,userID)
	return data ? data.items : []
}

async function addItem(h:string,userID:number,itemID:number,count?:number) {
	if(count == null || count <= 0) {
		count = 1
	}
	let config = await ItemHelper.getItemConfig(itemID)
	if(config == null) {
		return false 
	}
	switch(itemID) {
		case ItemID.gold: {
			return await Rpc.center.callException("kds.inpay.user.gold",userID,count)
		}
		case ItemID.diamond: {
			return await Rpc.center.callException("kds.inpay.user.diamond",userID,count)
		}
	}
	let data = await getData(h,userID)
	let items = data ? data.items : []
	do {
		if(config.pile) {
			let item = items.find(v=>v.itemID == itemID)
			if(item) {
				item.count += count 
				break 
			}
		} 
		if(!config.pile) {
			for(let i = 0 ; i < count ; i ++) {
				let newItem:ItemDefine.ItemPOData = {
					// 不可堆叠需要生成唯一key
					k:getItemKey(userID,itemID,i),
					itemID:itemID,
					count:1,
					extData:null 
				}
				items.push(newItem)
			}
		} else {
			let newItem:ItemDefine.ItemPOData = {
				//k:getItemKey(userID,itemID,count),
				itemID:itemID,
				count:count,
				extData:null 
			}
			items.push(newItem)
		}
	} while (false);
	if(data == null) {
		data = {
			userID:userID,
			items:items,
		}
		await db.insert(tableName,data)
	} else {
		await db.update(tableName,{userID:userID},data)
	}
	return true 
}

async function useItem(h:string,userID:number,itemID:number,count:number) {
	if(count == null || count <= 0) {
		count = 1
	}
	let data = await getData(h,userID)
	if(data == null) {
		return false 
	}
	let item = data.items.find(v=>v.itemID == itemID)
	if(item == null) {
		return false 
	}
	if(item.count < count) {
		return false 
	}
	item.count -= count
	if(item.count <= 0) {
		let idx = data.items.indexOf(item)
		data.items.splice(idx,1)
	}
	await db.update(tableName,{userID:userID},data)
	return true 
}

async function useItemKey(h:string,userID:number,key:string) {
	return await removeItemKey(h,userID,key)
}

async function removeItem(h:string,userID:number,itemID:number) {
	let data = await getData(h,userID)
	if(data == null) {
		return false 
	}
	let count = 0
	while(true) {
		let idx = data.items.findIndex(v=>v.itemID == itemID)
		if(idx < 0) {
			break 
		}
		count ++
		data.items.splice(idx,1)
	}
	if(count == 0) {
		return false 
	}
	await db.update(tableName,{userID:userID},data)
	return true 
}

async function removeItemKey(h:string,userID:number,key:string) {
	let data = await getData(h,userID)
	if(data == null) {
		return false 
	}
	let idx = data.items.findIndex(v=>v.k == key)
	if(idx < 0) {
		return false 
	}
	data.items.splice(idx,1)
	await db.update(tableName,{userID:userID},data)
	return true 
}

export let RpcItem = {
	getData:getData,
	add:addItem,
	getAll:getAllItems,
	use:useItem,
	useKey:useItemKey,
	remove:removeItem,
	removeKey:removeItemKey
}


async function setItemConfigs(h:string,items:ItemDefine.ItemConfigData[]) {
	await ItemHelper.setConfig(items)
	return true
}
async function getItemConfig(h:string,itemID:number) {
	let config = ItemHelper.getItemConfig(itemID)
	return config
}
async function getItemConfigs(h:string) {
	return await ItemHelper.getAllItemConfigs()
}
export let RpcItemConfig = {
	get:getItemConfig,
	setAll:setItemConfigs,
	getAll:getItemConfigs,
}